package com.runmi.webchat.aftersale.web.base;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.runmi.webchat.aftersale.common.utils.DESUtil;
import com.runmi.webchat.aftersale.common.utils.DateUtil;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.mapper.UserMapper;
import com.runmi.webchat.aftersale.util.Constants;



public class BaseController {

	protected static final Log log = LogFactory.getLog("logFile");
	protected static final Log logErr = LogFactory.getLog("errorLogFile");
	
	protected static final String ERROR_PAGE = "common/errorPage";
	
	@Autowired
	private UserMapper userMapper;
	
	
	protected String readCokie(HttpServletRequest request, HttpServletResponse response,
			String name) throws ServletException, IOException {
		String value = null;
		if (name != null) {
			Cookie cookies[] = request.getCookies();
			if (cookies != null && cookies.length >= 0) {
				for (int i = 0; i < cookies.length; i++) {
					Cookie cookie = cookies[i];
					if (name.equals(cookie.getName())) {
						value = cookie.getValue();
					}
				}
			}
		}
		return value;
	}
	
	protected String getTocken(String tocken){
		String result=null;
		try{
			//String tocken=this.readCokie(request, response, "scm_tocken");
			tocken = URLDecoder.decode(tocken, "UTF-8");
			tocken=DESUtil.decryptDES(tocken);
			return tocken;
			//String tocken=shopId + "-"  + userId  + "-"+ DateUtil.getToday("yyyyMMddHHmmss");
		}catch(Exception ex){
			
		}
		return result;
	}
	protected String getTocken(HttpServletRequest request, HttpServletResponse response){
		String result=null;
		try{
			String tocken=this.readCokie(request, response, Constants.SYSTEM_COOKIE);
			tocken = URLDecoder.decode(tocken, "UTF-8");
			tocken=DESUtil.decryptDES(tocken);
			return tocken;
			//String tocken=shopId + "-"  + userId  + "-"+ DateUtil.getToday("yyyyMMddHHmmss");
		}catch(Exception ex){
			
		}
		return result;
	}
	protected Integer getUserId(HttpServletRequest request, HttpServletResponse response){
		Integer result=null;
		try{
			String mytocken=getTocken(request,response);
			String[] tockens=mytocken.split("-");
			String uid=tockens[1];
			result=Integer.parseInt(uid);
			//String tocken=shopId + "-"  + userId  + "-"+ DateUtil.getToday("yyyyMMddHHmmss");
		}catch(Exception ex){
			
		}
		return result;
	}
	protected User getUserInfo(HttpServletRequest request, HttpServletResponse response){
		User result=null;
		try{
			Integer userId=getUserId(request,response);
			result= userMapper.selectByPrimaryKey(userId);
		}catch(Exception ex){
			
		}
		return result;
	}
	protected Integer getUserId(String tocken){
		Integer result=null;
		try{
			String mytocken=getTocken(tocken);
			String[] tockens=mytocken.split("-");
			String uid=tockens[1];
			result=Integer.parseInt(uid);
			//String tocken=shopId + "-"  + userId  + "-"+ DateUtil.getToday("yyyyMMddHHmmss");
		}catch(Exception ex){
			
		}
		return result;
	}
	protected User getUserInfo(String tocken){
		User result=null;
		try{
			Integer userId=getUserId(tocken);
			result= userMapper.selectByPrimaryKey(userId);
		}catch(Exception ex){
			
		}
		return result;
	}
	/**   
	 * 写Cookie
	 */
	protected void writeCokie(HttpServletRequest request, HttpServletResponse response,
			String name, String value, int days) throws ServletException, IOException {
		int day = 24 * 60 * 60;
		
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	protected void clearCokie(HttpServletRequest request, HttpServletResponse response,
			String name) throws ServletException, IOException {
		Cookie cookie = new Cookie(name, "");
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
}
